﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TaoPortal.BusinessLogic.DTO;
using TaoPortal.BusinessLogic.Extensions;
using TaoPortal.DataAccess;
using TaoPortal.Entity;

namespace TaoPortal.BusinessLogic.Repository
{
    public class ProdRepository : BaseRepository
    {
        public ProdRepository(DataContext dbcontext)
        {
            db = dbcontext;
        }
        public ProdRepository()
            : this(new DataContext())
        {

        }

        public PagedList<ProdList> GetProdInfoList(string pName, string pModel, string pNumber, string status, string pClass, string otherName, string otherValue, DateTime? startDate, DateTime? endDate, int pageIndex, int pageSize)
        {
            var data = (from p in db.TP_ProdInfo
                        join _other in db.TP_ProdOther on p.ID equals _other.ProdID into _others
                        where p.ClassID == pClass
                        && ((string.IsNullOrEmpty(otherName) || string.IsNullOrEmpty(otherValue)) || _others.Where(z => z.ProText == otherName && z.ProValue == otherValue).Count() > 0)
                        select p);
            if (!string.IsNullOrEmpty(pName))
            {
                data = data.Where(x => x.PName == pName);
            }
            if (!string.IsNullOrEmpty(pModel))
            {
                data = data.Where(x => x.PModel == pModel);
            }
            if (!string.IsNullOrEmpty(pNumber))
            {
                data = data.Where(x => x.PNumber == pNumber);
            }
            if (!string.IsNullOrEmpty(status))
            {
                data = data.Where(x => x.Status == status);
            }
            if (startDate != null)
            {
                data = data.Where(x => x.AddDate >= startDate);
            }
            if (endDate != null)
            {
                endDate = endDate.Value.AddDays(1);
                data = data.Where(x => x.AddDate < endDate);
            }
            return data.OrderByDescending(x => x.AddDate).Select(x => new ProdList
                        {
                            CreateTime = x.CreateTime,
                            ID = x.ID,
                            PModel = x.PModel,
                            PName = x.PName,
                            Status = x.Status
                        }).ToPagedList(pageIndex, pageSize);
        }

        public void AddProd(TP_ProdInfo prodInfo, List<TP_ProdImg> prodImgList, List<TP_ProdOther> others)
        {
            db.TP_ProdInfo.Add(prodInfo);
            foreach (var item in prodImgList)
            {
                db.TP_ProdImg.Add(item);
            }
            foreach (var item in others)
            {
                db.TP_ProdOther.Add(item);
            }
            SaveChanges();
        }

        public void DeleteProdByID(string id)
        {
            db.Entry<TP_ProdInfo>(new TP_ProdInfo { ID = id }).State = System.Data.Entity.EntityState.Deleted;
            var imgList = db.TP_ProdImg.Where(x => x.ProdID == id).ToList();
            foreach (var item in imgList)
            {
                db.TP_ProdImg.Remove(item);
            }
            var otherList = db.TP_ProdOther.Where(x => x.ProdID == id).ToList();
            foreach (var item in otherList)
            {
                db.TP_ProdOther.Remove(item);
            }
            SaveChanges();
        }

        public void DeleteProdByIDs(List<string> ids)
        {
            foreach (var id in ids)
            {
                db.Entry<TP_ProdInfo>(new TP_ProdInfo { ID = id }).State = System.Data.Entity.EntityState.Deleted;
                var imgList = db.TP_ProdImg.Where(x => x.ProdID == id).ToList();
                foreach (var item in imgList)
                {
                    db.TP_ProdImg.Remove(item);
                }
                var otherList = db.TP_ProdOther.Where(x => x.ProdID == id).ToList();
                foreach (var item in otherList)
                {
                    db.TP_ProdOther.Remove(item);
                }
            }
            SaveChanges();
        }

        public FullProdInfo GetProdByID(string id)
        {
            var data = (from p in db.TP_ProdInfo
                        join _pimg in db.TP_ProdImg on p.ID equals _pimg.ProdID into _pimgs
                        join _other in db.TP_ProdOther on p.ID equals _other.ProdID into _others
                        orderby p.CreateTime descending
                        select new FullProdInfo
                        {
                            TP_ProdInfo = p,
                            TP_ProdImg = _pimgs,
                            TP_ProdOther = _others
                        }).FirstOrDefault();
            return data;
        }

        public void UpdateProd(TP_ProdInfo prodInfo, List<TP_ProdImg> prodImgList, List<TP_ProdOther> others)
        {
            var oldProd = db.TP_ProdInfo.Where(x => x.ID == prodInfo.ID).FirstOrDefault();
            if (oldProd != null)
            {
                oldProd.PName = prodInfo.PName;
                oldProd.AddDate = prodInfo.AddDate;
                oldProd.Allowreply = prodInfo.Allowreply;
                oldProd.ClassID = prodInfo.ClassID;
                oldProd.Description = prodInfo.Description;
                oldProd.Introduction = prodInfo.Introduction;
                oldProd.PModel = prodInfo.PModel;
                oldProd.PNumber = prodInfo.PNumber;
                oldProd.PPrice = prodInfo.PPrice;
                oldProd.Status = prodInfo.Status;
                oldProd.TitleImage = prodInfo.TitleImage;
                oldProd.UpdateBy = prodInfo.UpdateBy;
                oldProd.UpdateTime = prodInfo.UpdateTime;

                var oldImg = db.TP_ProdImg.Where(x => x.ProdID == prodInfo.ID).ToList();
                foreach (var item in oldImg)
                {
                    db.TP_ProdImg.Remove(item);
                }
                foreach (var item in prodImgList)
                {
                    db.TP_ProdImg.Add(item);
                }
                var oldOther = db.TP_ProdOther.Where(x => x.ProdID == prodInfo.ID).ToList();
                foreach (var item in oldOther)
                {
                    db.TP_ProdOther.Remove(item);
                }
                foreach (var item in others)
                {
                    db.TP_ProdOther.Add(item);
                }
                SaveChanges();
            }
        }
    }
}
